home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3122 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  114 lines

  1. Path: ub239.dialup.uwa.edu.au!not-for-mail
  2. From: prye@cyllene.uwa.edu.au (Peter Rye)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: problems with a linked list
  5. Date: 26 Jan 1996 14:40:22 +0800
  6. Organization: The University of Western Australia
  7. Message-ID: <4e9t0m$re@ub239.dialup.uwa.edu.au>
  8. References: <1996Jan25.125329.23499@dcs.warwick.ac.uk>
  9. NNTP-Posting-Host: ub239.dialup.uwa.edu.au
  10.  
  11. D.C.Molero@dcs.warwick.ac.uk (Daniel Castillo Molero) writes:
  12.  
  13. Daniel,
  14.  
  15. The major problem is in your store_side function.
  16. Here you pass the function two pointers to struct side (s).
  17. The pointers are passed by value, therefore the function attempts
  18. to modify *copies* of the original pointers, which of course doesn't
  19. work.
  20.  
  21. You will no doubt also be told that main should be declared to return
  22. an int, and that you should always check the return values of your
  23. 'malloc' (s).
  24.  
  25. I've made a few adjustments and the code below compiles and runs without
  26. errors.
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30.  
  31. struct side
  32. {
  33.   int a;
  34.   struct side *next;
  35. };
  36.  
  37. struct side *i;
  38. struct side *first_side;
  39. struct side *tmp;
  40.  
  41. /* insert a new element in the list: use pointers to pointers */
  42. void store_side(struct side **i, struct side **first_side) {
  43.     if (*first_side == NULL) {
  44.     *first_side = *i;
  45.     (*first_side)->next = NULL; 
  46.     }
  47.     else { 
  48.     (*i)->next = *first_side; 
  49.     *first_side = *i; 
  50.     }
  51. }
  52.  
  53. /* Daniel's original store_side routine below:                   */
  54. /* >void store_side(struct side *i, struct side *first_side) {            */
  55. /* >  if (first_side == NULL) { first_side = i; first_side->next = NULL; }*/
  56. /* >  else { i->next = first_side; first_side = i; }               */
  57. /* >}                                      */
  58.  
  59.  
  60. int main(void) {    /* NOT void main(void) */
  61.  
  62. /* insert first element */
  63. first_side = NULL;
  64.  
  65. if((i = malloc(1 * sizeof(struct side))) == NULL) {  /* check malloc OK */
  66.     printf("malloc for \"i\" failed.\n");
  67.     exit(EXIT_FAILURE);
  68. }
  69.  
  70. (i->a) = 0;
  71. store_side(&i, &first_side);  /* pass pointer to pointer */
  72.  
  73. /* insert second element */
  74. if((i = malloc(1 * sizeof(struct side))) == NULL) {   /* ditto */
  75.     printf("malloc for \"i\" failed.\n");
  76.     exit(EXIT_FAILURE);
  77. }
  78.  
  79. (i->a) = 1;
  80. store_side(&i, &first_side);  
  81.  
  82. /* insert third elemenet */
  83. if((i = malloc(1 * sizeof(struct side))) == NULL) {  /* ditto */
  84.     printf("malloc for \"i\" failed.\n");
  85.     exit(EXIT_FAILURE);
  86. }
  87.  
  88. (i->a) = 2;
  89. store_side(&i, &first_side); 
  90.  
  91. /* print the three elements */
  92. tmp = first_side;
  93. printf("side: %d \n",tmp->a);
  94. while (tmp->next != NULL) {
  95.   tmp = tmp->next;
  96.   printf("side: %d \n",tmp->a);
  97. }
  98. return 0;    /* return an int */
  99. }
  100.  
  101. ub239:~/News/comp/lang/c$ gcc -ansi -pedantic -Wall -o junk test1.c
  102. ub239:~/News/comp/lang/c$ junk
  103. side: 2 
  104. side: 1
  105. side: 0
  106.  
  107. Hope this helps,
  108. Peter Rye
  109. -- 
  110. | Peter Rye                                                           |
  111. | Respiratory Research Fellow                                         |
  112. | Princess Margaret Hospital for Children                             |
  113. | Perth, Western Australia                                            |
  114.